package com.example.shixi.service;

import com.example.shixi.domain.SysUser;
import com.example.shixi.domain.SysUserExample;
import com.example.shixi.mapper.SysUserMapper;
import com.example.shixi.pojo.UserPage;
//import com.example.shixi.util.CopyUtil;
import com.example.shixi.util.JSONResult;
import com.example.shixi.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    private SnowFlake snowFlake = new SnowFlake();
    @Resource
    private SysUserMapper sysUserMapper;

    private SysUserExample sysUserExample = new SysUserExample();
    public SysUser selectByName(String name){
        SysUserExample sysUserExample1 = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample1.createCriteria();
        criteria.andUserNameEqualTo(name);
        List<SysUser> list = sysUserMapper.selectByExample(sysUserExample1);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }else{
            return list.get(0);
        }

    }
    public JSONResult list() {

        try {
            List<SysUser> list = sysUserMapper.selectByExample(sysUserExample);
            return JSONResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.error("卧槽，数据走丢了");
        }
    }

    public JSONResult save(SysUser user) {
        SysUser sysUser = selectByName(user.getUserName());
        if(ObjectUtils.isEmpty(sysUser)){
            user.setId(snowFlake.nextId());
            sysUserMapper.insert(user);
            return  JSONResult.success("添加成功");
        }else{
            return JSONResult.error("用户名已存在");
        }

    }

    public JSONResult login(SysUser user) {
        SysUser sysUser = selectByName(user.getUserName());
        if(ObjectUtils.isEmpty(sysUser)){
            return JSONResult.error("用户名不存在");
        }else{
            if(user.getPassword().equals(sysUser.getPassword())){
                return JSONResult.success(sysUser,"登录成功");
            }else{
                return JSONResult.error("密码错误");
            }
        }
    }

    public JSONResult del(Long id) {

        try {
            sysUserMapper.deleteByPrimaryKey(id);
            return JSONResult.success("删除成功了捏");
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("别问，问就是不知道");
        }

    }

    public JSONResult update(SysUser user) {
        try {
            sysUserMapper.updateByPrimaryKey(user);
            return JSONResult.success("成功了捏");
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("错误了呢宝儿");
        }
    }

    public JSONResult query(SysUser realName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        //SysUserExample sysUserExampleAll = new SysUserExample();
        UserPage<SysUser> userPage= new UserPage<>();

        //List<SysUser> sysUsersAll = sysUserMapper.selectByExample(sysUserExampleAll);
        if(!ObjectUtils.isEmpty(realName)){
            criteria.andRealNameLike("%"+realName.getRealName()+"%");
        }

        List<SysUser> sysUsers = sysUserMapper.selectByExample(sysUserExample);
        PageInfo<SysUser> pageInfo= new PageInfo<>(sysUsers);
        userPage.setList(sysUsers);
        userPage.setTotal(pageInfo.getTotal());
        userPage.setPageNum(pageNum);
        userPage.setPageSize(pageSize);
        return JSONResult.success(userPage);

    }
}
